'use strict';

const Controller = require('egg').Controller;
const _ = require('lodash');

class UserController extends Controller {
  async show() {
    const {ctx} = this;
    const loginname = ctx.params.loginname;
    const user = await ctx.service.userSvc.getUserByLoginName(loginname);

    if (!user) {
      ctx.status = 404;
      ctx.body = {success: false, error_msg: '用户不存在'};
      return;
    }

    const userId = user._id;
    const topics = await ctx.service.topicSvc.getTopicsByQuery({author_id: userId});
    const returnUser = _.pick(user, ['loginname', 'avatar_url', 'githubUsername', 'createdAt', 'score']);
    const returnTopics = topics.map(topic => {
      return {
        id: topic._id, last_reply_at: topic.last_reply_at, title: topic.title, author: {
          loginname: user.loginname, avatar_url: user.avatar_url,
        },
      };
    });
    const data = returnUser;
    data.recent_topics = returnTopics;
    ctx.body = {
      success: true, data,
    };
  }

  async verify() {
    const {ctx} = this;
    const user = ctx.request.user;
    ctx.body = {
      success: true, loginname: user.loginname, id: user._id, avatar_url: user.avatar_url,
    };
  }

  async login(ctx) {
    const body = ctx.request.body;
    const username = body.username;
    const password = body.password;

    if (!username || !password) {
      ctx.body = {success: false, msg: '请输入用户名或密码'};
      return;
    }
    const getUser = username => {
      if (username.indexOf('@') > 0) {
        return ctx.service.userSvc.getUserByMail(username);
      }
      return ctx.service.userSvc.getUserByLoginName(username);
    };
    const existUser = await getUser(username);

    // 用户不存在
    if (!existUser) {
      ctx.body = {success: false, msg: '用户不存在'};
      return;
    }

    const passhash = existUser.pass;
    // TODO: change to async compare
    const equal = ctx.helper.bcompare(password, passhash);
    // 密码不匹配
    if (!equal) {
      ctx.body = {success: false, msg: '密码不匹配'};
      return
    }

    // 用户未激活
    if (!existUser.active) {
      ctx.body = {success: false, msg: '用户未激活'};
      return;
    }

    // 验证通过
    ctx.body = {success: true, data: existUser.access_token};
    return;
  }


  async login4Chat(ctx) {
    const body = ctx.request.body;
    const username = body.username;
    const password = body.password;
    ctx.logger.info('login4Chat:', username);

    if (!username || !password) {
      ctx.body = {success: false, msg: '请输入用户名或密码'};
      return;
    }
    const getUser = username => {
      if (username.indexOf('@') > 0) {
        return ctx.service.userSvc.getUserByMail(username);
      }
      return ctx.service.userSvc.getUserByLoginName(username);
    };
    const existUser = await getUser(username);

    // 用户不存在
    if (!existUser) {
      ctx.body = {success: false, msg: '用户不存在'};
      return;
    }

    const passhash = existUser.pass;
    // TODO: change to async compare
    const equal = ctx.helper.bcompare(password, passhash);
    // 密码不匹配
    if (!equal) {
      ctx.body = {success: false, msg: '密码不匹配'};
      return
    }

    // 用户未激活
    if (!existUser.active) {
      ctx.body = {success: false, msg: '用户未激活'};
      return;
    }

    // 验证通过
    ctx.body = {
      success: true, data: encodeURIComponent(existUser.loginname) + 'G*qeA!Y$Znu7CN9%Y' + existUser.access_token
    };
    return;
  }


}

module.exports = UserController;
